#include "Tester.h"
#ifdef __CLOCKTESTER__ON

#include <Support\Timing\Clock.h>
using Timing::Clock;

TEST(Clock, Initialize)
{
	Clock clock;
	EXPECT_TRUE( clock.initialize() );
	EXPECT_TRUE( clock.shutdown() );
}

TEST(Clock, FrameTimeMeasuring)
{
	Clock clock;
#ifdef OVERNIGHT_TESTS
	EXPECT_TRUE( clock.initialize() );
	QTest::qSleep(1000);
	clock.lap();
	float timedTime = clock.lastLapTime();
	EXPECT_TRUE(0.9f < timedTime);
	EXPECT_TRUE(timedTime < 1.1f);
	clock.lap();
	QTest::qSleep(500);
	clock.lap();
	timedTime = clock.lastLapTime();
	EXPECT_TRUE(0.4f < timedTime);
	EXPECT_TRUE(timedTime < 0.6f);

	const int NUM_TEST = 10+rand()%100;
	const float THRESHOLD = 0.1f;
	for(int i=0; i<NUM_TEST; i++)
	{
		qDebug() << ".";
		int thisTestMIlliseconds = rand()%10000;
		float  thisTestSeconds = thisTestMIlliseconds / 1000.0f;
		clock.lap();
		QTest::qSleep(thisTestMIlliseconds);
		clock.lap();
		float elapsedSeconds = clock.lastLapTime();
		EXPECT_TRUE((thisTestSeconds-THRESHOLD)<elapsedSeconds);		
		EXPECT_TRUE(elapsedSeconds < (thisTestSeconds+THRESHOLD));
	}
#endif

	clock.lap();
	EXPECT_TRUE( clock.shutdown() );
}
#endif